package hanelsoft.vn.dictionaylearn.control.activity;

import hanelsoft.vn.dictionaylearn.R;
import hanelsoft.vn.dictionaylearn.control.custom.CustomVocabulary;
import hanelsoft.vn.dictionaylearn.control.custom.CustomVocabularyDelete;
import hanelsoft.vn.dictionaylearn.model.db.VocabularySQLiteHelper;
import hanelsoft.vn.dictionaylearn.model.entity.InfoVocabulary;
import hanelsoft.vn.dictionaylearn.model.entity.InfoVocabularyDelete;
import hanelsoft.vn.dictionaylearn.model.entity.InfoVocabularyEdit;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.LayoutParams;
import android.widget.ScrollView;
import android.widget.Toast;

public class Vocabulary extends Activity{
	ImageButton btnAdd, btnEdit, btnDelete, btnCancel, btnEdit_dialog, btnDelete_dialog;
	ListView list, list_dialog;
	ScrollView list_edit;
	InfoVocabulary info;
	InfoVocabularyEdit info_dialog;
	ArrayList<InfoVocabulary> array;
	ArrayList<InfoVocabularyDelete> array_delete;
	ArrayList<InfoVocabularyEdit> array_edit;
	VocabularySQLiteHelper mVocabularySQLiteHelper = new VocabularySQLiteHelper(this);;
	String Vocabulary_TAG = "[Vocabulary]";
	int positionItemEdit;
	int selectedIndex;
	String[] items;
	ArrayList<String> arrayitem;
	int VIEW_RADIO_ID = 12100702;
	@SuppressLint("DefaultLocale")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setInfoVocabulary();
	}
	private void setInfoVocabulary()
	{
		setContentView(R.layout.vocabulary);
		//
		array = new ArrayList<InfoVocabulary>();
		//
		list = (ListView)findViewById(R.id.vocabulary_list);
		btnAdd = (ImageButton)findViewById(R.id.vocabulary_add);
		btnEdit = (ImageButton)findViewById(R.id.vocabulary_edit);
		btnDelete = (ImageButton)findViewById(R.id.vocabulary_delete);
		btnCancel = (ImageButton)findViewById(R.id.vocabulary_cancel);
		//
		list.setAdapter(new CustomVocabulary(this, getInfoVocabulary()));
		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Intent mIntent = new Intent(Vocabulary.this, VocabularyContent.class);
				InfoVocabulary iv = getInfoVocabulary().get(position);
				mIntent.putExtra("name_table_vocabulary", iv.getName_table_vocabulary_content());
				startActivity(mIntent);
			}
		});
		btnAdd.setOnClickListener(new OnClickListener() {
			@SuppressLint("DefaultLocale")
			@Override
			public void onClick(View v) {
				LayoutInflater mLayoutInflater = LayoutInflater.from(Vocabulary.this);
				View mView = mLayoutInflater.inflate(R.layout.vocabulary_add_dialog, null);
				final EditText nameVocabulary = (EditText)mView.findViewById(R.id.vocabulary_add_name);
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Vocabulary.this);
		    	alertDialogBuilder.setView(mView);
		    	alertDialogBuilder.setTitle(R.string.create_name_list_vocabulary);
		    	alertDialogBuilder.setIcon(R.drawable.icon_add_list);
		    	alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
		            @Override
		            public void onCancel(DialogInterface dialog) {
		                finish();
		            }
		        }).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
					@SuppressLint("DefaultLocale")
					@Override
		            public void onClick(DialogInterface dialog, int which) {
		            	String nameListVocabulary = nameVocabulary.getText().toString();
		            	if(nameListVocabulary.equals("")== false)
		            	{
		            		String nameTableVocabulary = nameListVocabulary.toLowerCase();
			            	nameTableVocabulary = nameTableVocabulary.replace(" ", "");
			            	int check = 0;
			            	for(int i=0; i< getInfoVocabulary().size(); i++)
			            	{
			            		InfoVocabulary infoGetCheck = getInfoVocabulary().get(i);
			            		if(infoGetCheck.getName_table_vocabulary_content().equals(nameTableVocabulary))
			            		{
			            			check = 1;
			            		}
			            	}
				            if(check == 1)
				            {
				            	Toast.makeText(getBaseContext(), nameListVocabulary+getString(R.string.has_existed), Toast.LENGTH_SHORT).show();
				            }
				            else
				            {
				            	Log.i(Vocabulary_TAG, "nameListVocabulary: "+nameListVocabulary);
					           	mVocabularySQLiteHelper.createTableVocabulary(nameTableVocabulary);
					           	Log.i(Vocabulary_TAG, "Created: "+nameTableVocabulary);
					           	mVocabularySQLiteHelper.addTableVocabulary(nameListVocabulary, nameTableVocabulary);
					           	Log.i(Vocabulary_TAG, "Added: "+nameTableVocabulary);
					           	list.setAdapter(new CustomVocabulary(Vocabulary.this, getInfoVocabulary()));
				            }
			            	
		            	}
		            	else
		            	{
		            		Log.i(Vocabulary_TAG, "nameListVocabulary: "+nameListVocabulary);
		            	}
		            }
		        }).setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				});
		    	Dialog dialog = alertDialogBuilder.create();
		        dialog.show();
			}
		});
		btnEdit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setInfoVocabularyEdit();
			}
		});
		btnDelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setInfoVocabularyDelete();
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	@SuppressWarnings("deprecation")
	@SuppressLint("DefaultLocale")
	private void setInfoVocabularyEdit()
	{
		setContentView(R.layout.vocabulary_edit);
		//
		Log.i(Vocabulary_TAG, "get vacabulary sqlite");
		//
		list_edit = (ScrollView)findViewById(R.id.scrollView_list);
		btnAdd = (ImageButton)findViewById(R.id.vocabulary_add);
		btnEdit_dialog = (ImageButton)findViewById(R.id.vocabulary_edit_dialog);
		btnDelete = (ImageButton)findViewById(R.id.vocabulary_delete);
		list_edit = (ScrollView)findViewById(R.id.scrollView_list);
		btnCancel = (ImageButton)findViewById(R.id.vocabulary_cancel);
		btnCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setInfoVocabulary();
			}
		});
    	final RadioGroup mRadioGroup = new RadioGroup(this);
    	mRadioGroup.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT));
    	mRadioGroup.setOrientation(RadioGroup.VERTICAL);
    	for(int i=0; i<array.size(); i++)
    	{
    		RadioButton mRadioButton = new RadioButton(this);
    		mRadioButton.setId(VIEW_RADIO_ID + i);
    		InfoVocabulary infoedit = array.get(i);
    		mRadioButton.setText(infoedit.getName());
    		mRadioButton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    		mRadioGroup.addView(mRadioButton, i);
    	}
    	list_edit.addView(mRadioGroup);
		btnAdd.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LayoutInflater mLayoutInflater = LayoutInflater.from(Vocabulary.this);
				View mView = mLayoutInflater.inflate(R.layout.vocabulary_add_dialog, null);
				final EditText nameVocabulary = (EditText)mView.findViewById(R.id.vocabulary_add_name);
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Vocabulary.this);
		    	alertDialogBuilder.setView(mView);
		    	alertDialogBuilder.setTitle(R.string.create_name_list_vocabulary);
		    	alertDialogBuilder.setIcon(R.drawable.icon_add_list);
		    	alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
		            @Override
		            public void onCancel(DialogInterface dialog) {
		                finish();
		            }
		        }).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
					@SuppressLint("DefaultLocale")
					@Override
		            public void onClick(DialogInterface dialog, int which) {
		            	String nameListVocabulary = nameVocabulary.getText().toString();
		            	if(nameListVocabulary.equals("")== false)
		            	{
			            	String nameTableVocabulary = nameListVocabulary.toLowerCase();
			            	nameTableVocabulary = nameTableVocabulary.replace(" ", "");
			            	Log.i(Vocabulary_TAG, "nameListVocabulary: "+nameListVocabulary);
			            	int check = 0;
			            	for(int i=0; i< getInfoVocabulary().size(); i++)
			            	{
			            		InfoVocabulary infoGetCheck = getInfoVocabulary().get(i);
			            		if(infoGetCheck.getName_table_vocabulary_content().equals(nameTableVocabulary))
			            		{
			            			check = 1;
			            		}
			            	}
				            if(check == 1)
				            {
				            	Toast.makeText(getBaseContext(), nameListVocabulary+getString(R.string.has_existed), Toast.LENGTH_SHORT).show();
				            }
				            else
				            {
				            	mVocabularySQLiteHelper.createTableVocabulary(nameTableVocabulary);
				            	Log.i(Vocabulary_TAG, "Created: "+nameTableVocabulary);
				            	mVocabularySQLiteHelper.addTableVocabulary(nameListVocabulary, nameTableVocabulary);
				            	Log.i(Vocabulary_TAG, "Added: "+nameTableVocabulary);
			            	}
			            	setInfoVocabulary();
		            	}
		            }
		        }).setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				});
		    	Dialog dialog = alertDialogBuilder.create();
		        dialog.show();
			}
		});
		btnEdit_dialog.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LayoutInflater mLayoutInflater = LayoutInflater.from(Vocabulary.this);
				View mView = mLayoutInflater.inflate(R.layout.vocabulary_edit_dialog, null);
				final EditText nameVocabulary = (EditText)mView.findViewById(R.id.vocabulary_edit_name);
				selectedIndex = mRadioGroup.getCheckedRadioButtonId();
				if (selectedIndex < 0)
				{
					selectedIndex = 0;
				}
				if((selectedIndex - VIEW_RADIO_ID) >=0)
				{
				InfoVocabulary oldInfo = array.get(selectedIndex - VIEW_RADIO_ID);
				String oldName = oldInfo.getName();
				final String oldTableName = oldInfo.getName_table_vocabulary_content();
				final int oldId = oldInfo.getId();
				Log.i(Vocabulary_TAG, "select: "+oldName+", "+oldTableName+", id: "+oldId);
				nameVocabulary.setText(oldName);
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Vocabulary.this);
			    alertDialogBuilder.setView(mView);
			    alertDialogBuilder.setTitle(R.string.change_name_list_vocabulary);
			    alertDialogBuilder.setIcon(R.drawable.icon_edit_list);
			    alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
			         @Override
			         public void onCancel(DialogInterface dialog) {
			              finish();
			         }
			    }).setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
					@Override
			           public void onClick(DialogInterface dialog, int which) {
							{
								String nameListVocabulary = nameVocabulary.getText().toString();
								String nameTableVocabulary = nameListVocabulary.toLowerCase();
				            	nameTableVocabulary = nameTableVocabulary.replace(" ", "");
				            	if(nameTableVocabulary.equals("")== false);
				            	{
				            		if(nameTableVocabulary.equals(oldTableName)== false)
				            		{
				            			int check = 0;
						            	for(int i=0; i< getInfoVocabulary().size(); i++)
						            	{
						            		InfoVocabulary infoGetCheck = getInfoVocabulary().get(i);
						            		if(infoGetCheck.getName_table_vocabulary_content().equals(nameTableVocabulary))
						            		{
						            			check = 1;
						            		}
						            	}
							            if(check == 1)
							            {
							            	Toast.makeText(getBaseContext(), nameListVocabulary+getString(R.string.has_existed), Toast.LENGTH_SHORT).show();
							            }
							            else
							            {
							            	mVocabularySQLiteHelper.changeNameTableVocabulary(oldTableName,nameTableVocabulary);
								            Log.i(Vocabulary_TAG, "changed: "+nameTableVocabulary);
								            mVocabularySQLiteHelper.updateTableVocabulary(oldId, nameListVocabulary, nameTableVocabulary);
								            Log.i(Vocabulary_TAG, "updated: "+nameTableVocabulary);
							            	setInfoVocabulary();
							            }
				            		}
				            	}
							}
			           }
			       }).setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
					
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							
						}
					});
			    	Dialog dialog = alertDialogBuilder.create();
			        dialog.show();
				}
			}
		});
		btnDelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setInfoVocabularyDelete();
			}
		});
	}
	@SuppressLint("DefaultLocale")
	private void setInfoVocabularyDelete()
	{
		setContentView(R.layout.vocabulary_delete);
		//
		array_delete = new ArrayList<InfoVocabularyDelete>();
		//
		list_dialog = (ListView)findViewById(R.id.vocabulary_list);
		btnAdd = (ImageButton)findViewById(R.id.vocabulary_add);
		btnEdit = (ImageButton)findViewById(R.id.vocabulary_edit);
		btnDelete_dialog = (ImageButton)findViewById(R.id.vocabulary_delete_dialog);
		btnCancel = (ImageButton)findViewById(R.id.vocabulary_cancel);
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setInfoVocabulary();
			}
		});
		Log.i(Vocabulary_TAG, "get list for dialog");
		list_dialog.setAdapter(new CustomVocabularyDelete(Vocabulary.this, getInfoVocabularyDelete()));
		Log.i(Vocabulary_TAG, "set info list");
		list_dialog.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
			}
		});
		btnAdd.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LayoutInflater mLayoutInflater = LayoutInflater.from(Vocabulary.this);
				View mView = mLayoutInflater.inflate(R.layout.vocabulary_add_dialog, null);
				final EditText nameVocabulary = (EditText)mView.findViewById(R.id.vocabulary_add_name);
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Vocabulary.this);
		    	alertDialogBuilder.setView(mView);
		    	alertDialogBuilder.setTitle(R.string.create_name_list_vocabulary);
		    	alertDialogBuilder.setIcon(R.drawable.icon_add_list);
		    	alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
		            @Override
		            public void onCancel(DialogInterface dialog) {
		                finish();
		            }
		        }).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
					@SuppressLint("DefaultLocale")
					@Override
		            public void onClick(DialogInterface dialog, int which) {
		            	String nameListVocabulary = nameVocabulary.getText().toString();
		            	if(nameListVocabulary.equals("")== false)
		            	{
			            	String nameTableVocabulary = nameListVocabulary.toLowerCase();
			            	nameTableVocabulary = nameTableVocabulary.replace(" ", "");
				            mVocabularySQLiteHelper.createTableVocabulary(nameTableVocabulary);
				            Log.i(Vocabulary_TAG, "Created: "+nameTableVocabulary);
				            mVocabularySQLiteHelper.addTableVocabulary(nameListVocabulary, nameTableVocabulary);
				            Log.i(Vocabulary_TAG, "Added: "+nameTableVocabulary);
			            	setInfoVocabulary();
		            	}
		            }
		        }).setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				});
		    	Dialog dialog = alertDialogBuilder.create();
		        dialog.show();
			}
		});
		btnEdit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setInfoVocabularyEdit();
			}
		});
		btnDelete_dialog.setOnClickListener(new OnClickListener() {
			@SuppressLint("DefaultLocale")
			@Override
			public void onClick(View v) {
				LayoutInflater mLayoutInflater = LayoutInflater.from(Vocabulary.this);
				View mView = mLayoutInflater.inflate(R.layout.vocabulary_delete_dialog, null);
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Vocabulary.this);
		    	alertDialogBuilder.setView(mView);
		    	alertDialogBuilder.setTitle(R.string.delete_name_list_vocabulary);
		    	alertDialogBuilder.setIcon(R.drawable.icon_delete_list);
		    	alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener() {
		            @Override
		            public void onCancel(DialogInterface dialog) {
		                finish();
		            }
		        }).setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
					@SuppressLint("DefaultLocale")
					@Override
		            public void onClick(DialogInterface dialog, int which) {
						SharedPreferences share;
						share = Vocabulary.this.getSharedPreferences("CheckDelete", 1);
						ArrayList<InfoVocabularyDelete>arrayDelete = array_delete;
						for(int i=0; i<list_dialog.getAdapter().getCount(); i++)
						{
							int position = share.getInt("PositionCheckItem"+i, -1);
							if(position > -1)
							{
								InfoVocabularyDelete infoDelte = arrayDelete.get(position);
								int id = infoDelte.getId();
								String name= infoDelte.getNameword();
								Log.i(Vocabulary_TAG, "Delete: vocabulary at position: "+id);
								mVocabularySQLiteHelper.deleteTableVocabularyContent("vocabulary", id);
								Log.i(Vocabulary_TAG, "Delete table: "+name);
								mVocabularySQLiteHelper.deleteTableVocabulary(name);
							}
						}
						setInfoVocabulary();
		            }
		        }).setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					}
				});
		    	Dialog dialog = alertDialogBuilder.create();
		        dialog.show();
			}
		});
	}
	private ArrayList<InfoVocabulary> getInfoVocabulary() {
			Log.i(Vocabulary_TAG, "get getInfoVocabulary");
			array = mVocabularySQLiteHelper.viewAllVocabulary();
			return array;
		}
	private ArrayList<InfoVocabularyDelete> getInfoVocabularyDelete() {
		Log.i(Vocabulary_TAG, "get getInfoVocabularyDelete");
		array_delete = mVocabularySQLiteHelper.viewAllDeleteVocabulary();
		return array_delete;
		}
	@SuppressWarnings("unused")
	private ArrayList<InfoVocabularyEdit> getInfoVocabularyEdit() {
	Log.i(Vocabulary_TAG, "get getInfoVocabularyDelete");
	array_edit = mVocabularySQLiteHelper.viewAllEditVocabulary();
	return array_edit;
	}
}
